package yesjava;

import java.beans.Transient;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MainTest2 {
    public static void main(String[] args) throws Exception {

       /* Teacher instance = new Teacher();
        instance.sex = "男";*/

        Class clazz =Class.forName("yesjava.Teacher");// teacher.getClass();//Teacher.class;//
        Constructor constructor = clazz.getConstructor();
        Object instance = constructor.newInstance();

     /*   clazz.getMethod(); // 只能获取public 的成员
        clazz.getDeclaredMethod()*/ // 获取类代码中声明  的成员


//        clazz.getDeclaredField()
        Field field = clazz.getDeclaredField("sex");//clazz.getField("sex");//成员变量,核心方式设值
        field.setAccessible(true);
        field.set(instance,"班男");
        Method methodFn1 = clazz.getMethod("fn1", int.class, long.class);
        Object rs = methodFn1.invoke(instance, 1, 2L);
        System.out.println(rs);
/*
        Method methodFn2 = clazz.getMethod("fn2", int.class);
        methodFn2.invoke(null,99);
        */

        System.out.println(instance);


        System.out.println("========================");
        Method toStringMethod = clazz.getDeclaredMethod("toString");
        Transient annotation = toStringMethod.getDeclaredAnnotation(Transient.class);
        System.out.println((annotation!=null?"有帽子":"没有帽子"));
    }
}
/*

Constructor
Field
Method

getMethod
getDeclaredMethod

 */
